package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.pojo.Book;
import com.itany.service.BookListService;
import com.itany.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Author：侍家宇
 * Date：2019-09-18 下午2:07
 * Description：<描述>
 */


/**
 * 书籍管理查询
 */
@Controller
@RequestMapping("book")
public class BookListController {
    @Autowired
    private BookListService bookListService;


    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer rows) {

        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<Book> info = bookListService.findBookAll(page,rows);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());

        return map;

    }
    /**
     * 书籍管理状态修改
     * 启用
     */

        @ResponseBody
        @RequestMapping("/enable")
    public AjaxResult Enable(Integer id){
            AjaxResult result = new AjaxResult();

            try {
                bookListService.modifyEnable(id);
                result.setSuccess(true);
                result.setMsg("成功");
            } catch (Exception e) {
                e.printStackTrace();
                result.setSuccess(false);
                result.setMsg("服务器异常");
            }
            return result;
        }
    /**
     * 书籍状态管理
     * 禁用
     */
    @ResponseBody
    @RequestMapping("/disable")
    public  AjaxResult Disable(Integer id){
        AjaxResult result = new AjaxResult();

        try {
            bookListService.modifyDisable(id);
            result.setSuccess(true);
            result.setMsg("成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg("服务器异常");
        }
            return result;

    }
    /**
     * 书籍管理
     * 修改前的查询
     */
    @ResponseBody
    @RequestMapping("/findbook")
    public AjaxResult findBook(Integer id){
        AjaxResult result = new AjaxResult();

        try {
         Book book=bookListService.findBook(id);
        result.setObj(book);
        result.setSuccess(true);
        result.setMsg("成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg("服务器异常");
        }

        return result;
    }

    /**
     * 书籍管理
     * 书籍信息修改
     */

    @RequestMapping("/modifybook")
    public String modifyBook(Book book){

        System.out.println("................"+book.toString());
        try {
        bookListService.modifyBook(book);


        } catch (Exception e) {
            e.printStackTrace();


        }
            return "redirect:/manager/showbooklist";
    }
}
